<?php
class Product_categoryAction extends CommonAction {
	function index(){
		$this->listmap.=" and pid=0";
		$this->listorder ="sort,id desc";
		if($this->_param('keywords')){
			$keywords=$this->_param('keywords');
			$_GET['keywords']=urlencode($this->_param('keywords'));
			$this->listmap.=" and (title like '%{$keywords}%')";
			$this->keywords=$keywords;
		}
        parent::_list();
		$Category=D('Product_category'); 
		$list=$this->list;
		foreach($list as $k=>$v){
			$list[$k]['sub']=$Category->getListToArray1($v['id'],$this->siteid);
		}
		
		$this->assign('list',$list);
		$this->display();
	}
	public function delete(){
		if($this->_get('id')){
			$map['pid']=$this->_get('id');
			$re=$this->dao->where($map)->find();
			if($re){
				$this->error('该分类包含了子类,请先删除子类');
			}else{
				$all=$this->dao->where('id in('.$this->_get('id').')')->select();
				foreach($all as $k=>$v){
					//删除图片
					$this->delImgBypath($v['img_url']);
				}
				parent::delete();
			}
		}
	}
	private function treehtml($pid = 0){
		$list = $this->dao -> where(array('site_id' => __SITEID__, 'pid' => $pid)) -> order('sort') -> select();
		if($list){
			$Productdb = M('Product');
			foreach($list as $k=>$v){
				
			    $list[$k]['goodscount'] = $Productdb -> where(array('category_id' => $v['id'], 'site_id' => $this->siteid)) -> count(); 
		    } 
			$this->assign('list', $list); 
			$curstr = $this->fetch('indextree');
			foreach($this->list as $k => $v){
			     $curstr = str_replace('<!--sub#'.$v['id'].'-->', $this->treehtml($v['id']), $curstr);
		    }
			return $curstr;
		}
	}
	private function parsesortdata($arr = array(), $ret = array(), $pid = 0){
		$pid1 = $pid;
		foreach($arr as $k => $v){
			$p['pid'] = $pid1;
			$p['id'] = $v['id'];
			$ret[] = $p;
			if(isset($v['children'])){
				$pid = $v['id'];
				$ret = $this->parsesortdata($v['children'], $ret, $pid);
			}
		}
		return $ret;
	}
	
	public function updatesort($data){
		$data = $this->parsesortdata(json_decode($_POST['data'], true));
		foreach($data as $k => $v){
			$map['id'] = $v['id'];
			$map['site_id'] = $this->siteid;
			$data['sort'] = $k + 1;
			$data['pid'] = $v['pid'];
			$this->dao -> where($map) -> save($data);
		}
	}
	
	function indextree(){
		$pid = $this->_get('pid') ? $this->_get('pid') : 0;
		$this->listmap.=" and pid=".$pid;
		$this->listorder = 'sort';
		if($this->_param('keywords')){
			$keywords=$this->_param('keywords');
			$_GET['keywords']=urlencode($this->_param('keywords'));
			$this->listmap.=" and (title like '%{$keywords}%' or name like '%{$keywords}%')";
			$this->keywords=$keywords;
		}
		$this->initView();
        parent::_list();
		$list=$this->list;
		$Productdb = M('Product');
		foreach($list as $k=>$v){ 
			$list[$k]['goodscount'] = $Productdb -> where(array('category_id' => $v['id'], 'site_id' => $this->siteid)) -> count(); 
		}
		$this->assign('list',$list);
		$str = $this->fetch();
		$list = $this->view -> get('list');
		foreach($list as $k => $v){
			$str = str_replace('<!--sub#'.$v['id'].'-->', $this->treehtml($v['id']), $str);
		}
		
		$list = $_GET['list'];
		$empty = 1;
		$statu=$list?1:0;
		if($this->get('list')){
			$empty = 0;
		}
		
		die(json_encode(array('empty' => $empty,'tbody'=>$str,'dump'=>$_REQUEST,'statu'=>$statu, 'pages' => $this-> view -> get('pages'))));
	}
	
	
	public function tree(){ 
		$map['pid'] = 0;
		if($this->_get('pid')){
			$map['pid'] = $this->_get('pid');
		}
		$map['site_id'] = __SITEID__;
		$list = $this->dao -> where($map) -> Field('title as name,id') -> select();
		foreach($list as $k => $v){
			//查找有无下级
			$sub = $this-> dao -> where(array('site_id' => __SITEID__, 'pid' => $v['id'])) -> find();
			$list[$k]['type'] = $sub ? 'folder' : 'item';
			$list[$k]['additionalParameters'] = array('id' => $v['id']);
		}
		echo json_encode($list);
	}
	public function add(){
		$pid_list=$this->dao->where(array('pid'=>0,'site_id'=>$this->siteid))->select();
		$category=D('Product_category');
		foreach($pid_list as $k=>$v){
			$pid_list[$k]['sub']=$this->dao->getListToArray1($v['id'],$this->siteid);
		}
		
		$this->brandList = M('Brand') -> where(array('site_id' => $this->siteid)) -> select();
		$type_list=M('Type')->where(array('site_id'=>$this->siteid))->select();
		$this->assign('type_list',$type_list);
		$this->assign('pid_list',$pid_list);
		if($this->_get('id')){
			$this->assign('id',$this->_get('id'));
		}
		parent::add();
	}
	
	public function insert(){
		if(isset($_POST['name'])&&trim($_POST['name'])==''){
			Vendor('Pinyin.pinyin');
			$py=new cn2pinyin();
			$_POST['name']=$py->get($_POST['title']);
		}
		$_POST['site_id']=$this->siteid;
		parent::insert();
	}
	public function edit(){
		$pid_list=$this->dao->where(array('pid'=>0,'site_id'=>$this->siteid))->select();
		foreach($pid_list as $k=>$v){
			$pid_list[$k]['sub']=$this->dao->getListToArray1($v['id'],$this->siteid);
		}
		$this->brandList = M('Brand') -> where(array('site_id' => $this->siteid)) -> select();
		$type_list=M('Type')->where(array('site_id'=>$this->siteid))->select();
		$this->assign('type_list',$type_list);
		$this->assign('pid_list',$pid_list);
		
		$this->getLevelNameList = D('Member') -> getLevelNameList();
		if ($this->_get('id')) {
            $map['id'] = array(
                'eq',
                $this->_get('id')
            );
			
			
			
            $info = $this->dao->where($map)->find();
			$info['brands'] = explode(',', trim($info['brands'], ','));
			$this->assign('info', $info);
            $this->display(); 
        } else {
            $this->error('参数错误');
        }
		
	}
	
	//重写更新方法,防止用户选择自己作为父分类
	public function update(){
		if(isset($_POST['name'])&&trim($_POST['name'])==''){
			Vendor('Pinyin.pinyin');
			$py=new cn2pinyin();
			$_POST['name']=$py->get($_POST['title']);
		}
		
		$_POST['brands'] = ','.implode(',', $_POST['brands']).',';
		
		if($this->_post('pid')==$this->_post('id')){
			$this->error('您不能选择当前分类作为父类');
		}else{
			parent::update();
		}
	}
	
	
	
	
	//模块选择栏目数据
	public function selectCategory(){
		if($this->_post('id')){
			$modulesdb = M('Modules');
			$id = $this->_post('id');
			$module = $modulesdb->where(array('id' => $this->_post('modules_id'),'site_id' => $this->siteid))->find();
			//echo $modulesdb->getLastSql();
			if(is_array($module)){
				$modules = unserialize($module['content']);
				$newdata = array();
				$isex = false;
				foreach($modules as $k => $v){
					if($v['id'] != $id || $v['module'] == 0){
					    $newdata[] = $v;
					}else{
						$isex = true;
					}
					//是否已经存在记录,是就取消
				}
				//记录不存在增加一条记录
				if(!$isex){
				    $newdata[] = $this->dao->where(array('id' => $id,'site_id' => $this->siteid))->find();
				}
				$ret = $modulesdb->data(array('content' => serialize($newdata)))->where(array('id' => $this->_post('modules_id'),'site_id' => $this->siteid))->save();
				if($ret !== false){
					$data['status'] = 1;
					$data['info'] = '操作成功';
					if(!$isex){
						//新增
						$data['type'] = 1;
					}else{
						$data['type'] = 0;
					}
					exit(json_encode($data));
				}else{
					$this->error($modulesdb->getError());
				}
				
			}else{
				$this->error('当前站点没有这个模块');
			}
			exit();
		}
		if($this->_get('modules_id')){
			$module = M('Modules')->where(array('id' => $this->_get('modules_id'),'site_id' => $this->siteid))->find();
			if(is_array($module)){
				$modules = unserialize($module['content']);
				$selectedid = array();
				foreach($modules as $k => $v){
					if($v['module'] == 1)
					$selectedid[] = $v['id'];
				}
				$this->assign('modules_id', $this->_get('modules_id'));
				$this->assign('selectedid',$selectedid);
			}else{
				$this->error('当前站点没有这个模块'); 
			}
		}else{
			$this->error('模块ID出错,参数错误');exit;
		}
		//显示列表
		if(isset($_GET['keywords']) && $_GET['keywords'] != ''){
			$this->listmap.=" and title like '%".$_GET['keywords']."%'";
		}else{
			$this->listmap.=" and pid=0";
		}
		//$this->listmap.=" and pid=0";
        parent::_list();
		$Category=D('Product_category');
		$list=$this->list;
		foreach($list as $k=>$v){
			$list[$k]['sub']=$Category->getListToArray1($v['id'],$this->siteid);
		}
		$this->assign('list',$list);
		$this->display();
	}
	
	
	
	
	
}

?>